package com.zhoushihao.class0820;

import java.io.*;
import java.util.*;

public class Admission {
    public static void main(String[] args) {

        //1.读取大学计划录取
        Map<String, Integer> planMap = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new FileReader("./src/com/neusoft/zhoushihao/class0820/universities.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] arr = line.split(",");
                planMap.put(arr[0], Integer.parseInt(arr[1]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //2.读取学生信息
        List<Student> studentList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("./src/com/neusoft/zhoushihao/class0820/scores.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] arr = line.split(",");
                Student student = new Student(arr[0], arr[1], Integer.parseInt(arr[2]));
                studentList.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //3.录入学生志愿书(遍历一个目录下的所有文件，案文件名的学号存入到对应的学生的chioceslist中)
        File file = new File("E:/JAVA/JAVAClass202508/src/com/neusoft/zhoushihao/class0820/choices");
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                String[] arr = file1.getName().split("\\.");
                String id = arr[0];
                try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file1.getAbsoluteFile()))) {
                    List<String> choices = new ArrayList<>();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        choices.add(line);
                    }
                    for (Student student : studentList) {
                        if (student.id.equals(id)) {
                            student.getChoices(choices);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        //4.学生按成绩排序依次录取
        for (Student student : studentList) {
            List<String> choices = student.choices;
            for (String choice : choices) {
                if (planMap.get(choice) > 0) {
                    student.getAdmittedUniversity(choice);
                    int count = planMap.get(choice);
                    count--;
                    planMap.put(choice, count);
                    break;
                }
            }
        }
        //5.输出录取结果
        Map<String, List<Student>> admittedMap = new LinkedHashMap<>();
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("./src/com/neusoft/zhoushihao/class0820/result.txt"))) {
            for (Student student : studentList) {
                if (student.admittedUniversity != null) {
                    bufferedWriter.write(student.id + "," + student.name + "," + student.score + "," + student.admittedUniversity);
                    bufferedWriter.newLine();
                    List<Student> students = admittedMap.getOrDefault(student.admittedUniversity, new ArrayList<>());
                    students.add(student);
                    admittedMap.put(student.admittedUniversity, students);
                } else {
                    bufferedWriter.write(student.id + "," + student.name + "," + student.score + ",未录取");
                    bufferedWriter.newLine();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("./src/com/neusoft/zhoushihao/class0820/admitted.txt"))) {
            for (Map.Entry<String, List<Student>> entry : admittedMap.entrySet()) {
                bufferedWriter.write(entry.getKey() + " 已录取学生：");
                bufferedWriter.newLine();
                for (Student student : entry.getValue()) {
                    bufferedWriter.write(student.id + "," + student.name + "," + student.score);
                    bufferedWriter.newLine();
                }
                bufferedWriter.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
